package com.example.user.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.example.mapper.GdUserMapper;
import com.example.pojo.GdUser;
import com.example.service.RpcLoginService;
import com.example.utils.MD5;
import com.example.utils.RedisUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component
@Service(interfaceClass= RpcLoginService.class)
public class RpcLoginServiceImpl implements RpcLoginService {

    @Resource
    private GdUserMapper qgUserMapper;

    @Resource
    private RedisUtils redisUtils;

    @Override
    public GdUser getGdUserById(String id) throws Exception{
        return qgUserMapper.selectByPrimaryKey(Long.parseLong(id));
    }

    @Override
    public GdUser findByUserCode(String userCode) throws Exception {
        return qgUserMapper.findByUserCode(userCode);
    }

    @Override
    public GdUser login(String userCode, String password) throws Exception {
        GdUser user=this.findByUserCode(userCode);
        if(null!=user&&user.getUserpassword().equals(MD5.getMd5(password,32))){
            return user;
        }else{
            return null;
        }
    }
    @Override
    public GdUser getCurrentUser(String tokenString) throws Exception {
        GdUser qgUser = null;
        try{
            if(null == tokenString || "".equals(tokenString)){
                return null;
            }
            String userInfoJson = redisUtils.getValue(tokenString).toString();
            qgUser = JSONObject.parseObject(userInfoJson,GdUser.class);
        }catch(Exception e){
            e.printStackTrace();
            return qgUser;
        }
        return qgUser;
    }
}
